<?php

/**
 * Framework_DB_RelationCollection
 *
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
 * @see Framework_Module
 * @filesource
 */


class Framework_DB_RelationCollection extends Framework_DB_DaoCollection {


	/**
	 * Constructor
	 */
	public function __construct( $aClassName, $aParameterSet=null, $aPager=null ) {
		parent::__construct( $aClassName, $aParameterSet, $aPager ) ;
	}// end constructor



	/**
	 * Empty the collection
	 */
	public function purgeData() {
		$this->_data = array() ;
		$this->_size = 0 ;
	}// end purgeData




	/**
	 * Replace the collection's _data array
	 * @todo Verify whether transaction code really works in MySQL (line 94)
	 */
	public function replaceData( $aReplacementCollection ) {

		$_output = false ;
		$transactionSql = "" ;
		$values = array();


		if( $aReplacementCollection instanceof Framework_DB_RelationCollection ) {
			$rc = $aReplacementCollection ;
			switch( $this->db->dsn['dbsyntax'] ) {

				case DSN_MSSQL:
					$transactionSql = "BEGIN TRANSACTION" . LINE_END_MSSQL ;

					for( $i=0; $i<$this->_size; $i++ ) {
						$nextNode = $this->_data[$i] ;
						$remove = $nextNode->getRemoveStatement( true );
						$transactionSql .= $remove['sql'] . LINE_END_MSSQL;
						$values = array_merge($values, $remove['values']);
					}

					for( $i=0; $i< $rc->_size; $i++ ) {
						$nextNode = $rc->_data[$i] ;
						$create = $nextNode->getCreateStatement( true );
						$transactionSql .= $create['sql'] . LINE_END_MSSQL;
						$values = array_merge($values, $create['values']);
					}

					$transactionSql .= "COMMIT TRANSACTION" . LINE_END_MSSQL ;
					$preparedSql = $this->db->prepare($transactionSql);
					$result = $preparedSql->execute( $values ) ;
					$preparedSql->free();

					break ;


				case DSN_MYSQL:
				case DSN_OCI:
					$this->db->query("START TRANSACTION");
					
					for( $i=0; $i<$this->_size; $i++ ) {
						$nextNode = $this->_data[$i] ;
						$remove = $nextNode->getRemoveStatement( true );
						$preparedSql = $this->db->prepare($remove['sql']);
						$result = $preparedSql->execute( $remove['values'] ) ;
						$preparedSql->free();
					}

					for( $i=0; $i<$rc->_size; $i++ ) {
						$nextNode = $rc->_data[$i] ;
						$create = $nextNode->getCreateStatement( true );
						$preparedSql = $this->db->prepare($create['sql']);
						$result = $preparedSql->execute( $create['values'] ) ;
						$preparedSql->free();
					}

					$result = $this->db->query( "COMMIT" ) ;

					break ;
			}// end switch

			if( Framework_ErrorHandler::isError( $result, __METHOD__."::".__LINE__ )) {
				$_output = $result ;
			} else {
				$this->_data = $rc->_data ;
				$this->_size = $rc->_size ;
				$_output = true ;
			}

		} else {
			$_output = Framework_ErrorHandler::raiseError( 
					__METHOD__.":: Supplied argument is not a valid instance of Framework_DB_RelationCollection.", 
					__METHOD__."::".__LINE__ 
					) ;
		}

		return $_output ;
	}// end replaceData



	/**
	 * Add a relation to the collection
	 */
	public function add( $aRelation ) {
		if( $aRelation instanceof Framework_DB_DaoRelation ) {
			$this->_data[] = $aRelation ;
			$this->_size ++ ;
		} else {
			Framework_Util_Debug::show( array( __METHOD__, "Supplied argument is not a valid Framework_DB_DaoRelation object." )) ;
		}
	}



	/**
	 * Desctructor
	 */
	public function __destruct() {
		parent::__destruct() ;
	}
}

?>
